import time
import pytest
import allure
import TestCase

from Base.InitDriver import init_driver
from Page.CommonPage import CommonPage
from Utils.XiaoMaUtils import XiaoMaUtils


from Utils.PerfDogUtils import PerfDogUtils


@allure.feature('TestCase_06: 播放电台1H消耗的流量在合理范围之内	')
@allure.issue(
    'https://www.tapd.cn/56767367/sparrow/tcase/view/1156767367001062273?url_cache_key=ab49d6eabe9823050826460ca132aea4')
class TestCaseSpecial06:
    def setup_class(self):
        self.driver = init_driver()
        # 实例化页面
        # 桌面
        self.launcher_index_page = CommonPage(self.driver).get_launcher_index_page()
        self.launcher_app_list_page = CommonPage(self.driver).get_launcher_app_list_page()
        # 音乐
        self.xting_index_page = CommonPage(self.driver).get_xting_index_page()
        self.xting_choice_page = CommonPage(self.driver).get_xting_choice_page()

        # 实例化PerfDogUtils的工具类
        # 开始监控
        self.perfDogUtils = PerfDogUtils()
        # 输入需监控的应用名, 如"xting", "music"
        self.perfDogUtils.start_test("xting")

    def teardown_class(self):
        self.perfDogUtils.stop_test()
        XiaoMaUtils.close_app('xting')
        XiaoMaUtils.close_app('launcher')
        XiaoMaUtils.clear_data('xting')
        self.driver.quit()

    @allure.story('step1:点击应用页,点击电台图标')
    @pytest.mark.run(order=61)
    def test_special_06_1_click_list(self):
        try:
            self.launcher_index_page.click_app_list()
            self.launcher_app_list_page.click_appname(TestCase.XTING)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story('step2:进入电台,查看页面加载完成')
    @pytest.mark.run(order=62)
    def test_special_06_2_click_list(self):
        try:
            text1 = self.xting_choice_page.is_display_title()
            if text1 == True:
                time.sleep(5)
                self.xting_choice_page.click_skip()
                self.xting_index_page.is_display_benteng_radio()
                text2 = self.xting_index_page.get_ele_text(self.xting_index_page.get_list_radio_title(index=2))
                if text2:
                    print("列表加载成功")
                else:
                    print("请检查网络是否正常")
            else:
                self.xting_choice_page.click_skip()
                self.xting_index_page.is_display_benteng_radio()
                text2 = self.xting_index_page.get_ele_text(self.xting_index_page.get_list_radio_title(index=2))
                if text2:
                    print("列表加载成功")
                else:
                    print("请检查网络是否正常")
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story('step3:播放一个小时,查看流量消耗')
    @pytest.mark.run(order=63)
    def test_special_06_3_wait(self):
        try:
            self.xting_index_page.click_list_radio(index=5)
            print("当前正在测试流量,请不要结束进程和更改页面")
            time.sleep(15)
            # time.sleep(3600)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)


if __name__ == 'main__':
    # pass
    pytest.main("-s testcase_special_xting_06.py")
